<?php
/**
 * 品牌管理
 */
namespace app\admin\controller;

use think\Request;
use app\admin\service\Brand as BrandService;
use app\admin\validate\Brand as BrandValidate;

class Brand extends BaseController
{
    /**
     * 获取品牌列表
     * @param Request $request
     * @param BrandService $brandService
     * @param BrandValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request,BrandService $brandService,BrandValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] =  $request->param('name',null);
        $param['start_date'] =  $request->param('start_date',null);
        $param['end_date'] =  $request->param('end_date',null);

        $intPage =  $request->param('pageIndex',1);
        $intPageSize =  $request->param('pageSize',50);
        $arrData = $brandService->getList($param,(int)$intPage,(int)$intPageSize);
        return json($arrData);
    }

    /**
     * 品牌添加和修改
     * @param Request $request
     * @param BrandService $brandService
     * @param BrandValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request,BrandService $brandService,BrandValidate $validate){
        $checkData = $request->param();
        $checkType = isset($checkData['id']) ? 'save' : 'create';
        if (!$validate->scene($checkType)->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['name'] =  trim($request->param('name',null));
        $param['image'] =  $request->param('image',null);
        $param['sort_order'] =  $request->param('sort_order',null);
        $param['description'] =  trim($request->param('description',null));
        $param['firmware_url'] =  $request->param('firmware_url',null);
        $param['id'] =  $request->param('id',null);
        $arrData = $brandService->save($param);
        return json($arrData);
    }

    /**
     * 品牌详情
     * @param Request $request 品牌ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request,BrandService $brandService,BrandValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $arrData = $brandService->getInfo($param);
        return json($arrData);
    }


    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param BrandValidate $validate
     * @param BrandService $brandService
     * @return \think\response\Json
     */
    public function delete(Request $request,BrandValidate $validate,BrandService $brandService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids',null);
        $arrData = $brandService->delete($param);
        return json($arrData);
    }

    public function getSelect(Request $request,BrandService $brandService,BrandValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $arrData = $brandService->getSelect();
        return json($arrData);
    }
}
?>
